package com.onlinesshop.mall.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 叶建雄
 * @date 2019/12/23 11:51
 */


public class MD5 {
    /**
     * MD5加密一次
     * @param content 需要加密的字符串
     * @return 返回加密后的字符串
     */
    public static String getMD5(String content) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(content.getBytes());
            return getHashString(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getHashString(MessageDigest digest) {
        StringBuilder builder = new StringBuilder();
        for (byte b : digest.digest()) {
            builder.append(Integer.toHexString((b >> 4) & 0xf));
            builder.append(Integer.toHexString(b & 0xf));
        }
        return builder.toString();
    }

    /**
     *
     * @param content 需要加密的字符串
     * @param n MD5循环加密次数
     * @return 返回加密后的字符串
     */
    public static String getMD5(String content,int n){
        String s1 = content;
        for(int i = 0;i < n;i++){
            s1 = getMD5(s1);
        }
        return s1;
    }

}
